package so.sao.code.service.infrastructure.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import so.sao.code.service.infrastructure.SessionContext;
import so.sao.code.service.infrastructure.annotation.OperationLog;
import so.sao.code.service.infrastructure.annotation.Permission;
import so.sao.code.service.infrastructure.enums.CodeMessage;
import so.sao.code.service.infrastructure.enums.ROLE;
import so.sao.top.core.util.rest.RestException;

/**
 * Created by Administrator on 2017/10/19.
 */
@Aspect
@Component
public class SystemAspect {
	@Autowired
	SessionContext context;

	@Before(value = "within(so.sao.code.service.service.impl..*) && @annotation(permission)")
	public void doBefore_Permission(JoinPoint jp, Permission permission) {
		if (permission.role() == ROLE.ADMIN && !context.CURRECT_USER.getEseId().equals(1L))
			throw new RestException(HttpStatus.BAD_REQUEST, CodeMessage.InvalidRequest);

	}

	// @Before(value = "within(so.sao.code.service.service.impl..*) &&
	// @annotation(operationlog)")
	public void doBefore_OperationLog(JoinPoint jp, OperationLog operationlog) {

	}
}
